package logic

import (
	"context"
	"encoding/json"
	"time"

	"hzjt-oam/rpc/sys/internal/svc"
	"hzjt-oam/rpc/sys/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchSysRoleDeptLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchSysRoleDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchSysRoleDeptLogic {
	return &SearchSysRoleDeptLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SearchSysRoleDeptLogic) SearchSysRoleDept(in *pb.SearchSysRoleDeptReq) (*pb.SearchSysRoleDeptResp, error) {
	count, _ := l.svcCtx.RoleDeptModel.Count()
	all, err := l.svcCtx.RoleDeptModel.FindAll(1, count)

	if err != nil {
		reqStr, _ := json.Marshal(in)
		logx.WithContext(l.ctx).Errorf("查询角色列表信息失败,参数:%s,异常:%s", reqStr, err.Error())
		return nil, err
	}

	var list []*pb.SysRoleDept
	for _, role := range *all {
		list = append(list, &pb.SysRoleDept{
			Id:             role.Id,
			RoleId:         role.RoleId,
			DeptId:         role.DeptId,
			CreateBy:       role.CreateBy,
			CreateTime:     time.Now().Unix(),
			LastUpdateBy:   role.LastUpdateBy,
			LastUpdateTime: time.Now().Unix(),
		})
	}

	reqStr, _ := json.Marshal(in)
	listStr, _ := json.Marshal(list)
	logx.WithContext(l.ctx).Infof("查询角色列表信息,参数：%s,响应：%s", reqStr, listStr)

	return &pb.SearchSysRoleDeptResp{SysRoleDept: list}, nil
}
